import { isArray, ShapeFlags } from '@/shared'

export function initSlots(instance, children) {
  // instance.slots = isArray(children) ? children : [children]
  const { vnode } = instance
  if (vnode.shapeFlag & ShapeFlags.SLOTS_CHILDREN) {
    normalizeObjectSlots(children, instance.slots)
  }

  function normalizeObjectSlots(children, slots) {
    for (const key in children) {
      const value = children[key]
      slots[key] = (props) => normalizeSlotValue(value(props))
    }
  }
}

function normalizeSlotValue(value) {
  // 把 function 返回的值转换成 array ，这样 slot 就可以支持多个元素了
  return isArray(value) ? value : [value]
}
